<template>
  <view>
    <BaseNavbar title="设备广告" />
    <BaseSearch placeholder="请输入设备编号" @search="search" />
    <ComList
      :loadingType="loadingType"
      :bottom="isFromAdSelectDevice ? 120 : 0"
    >
      <DeviceAdvertsCard
        v-for="item in listData"
        :key="item.device_sn"
        :info="item"
        :isShowCheck="isFromAdSelectDevice"
        @refresh="refresh"
        @onCheck="selectItem(item)"
      />
    </ComList>
    <FixedSubButton
      v-if="isFromAdSelectDevice"
      :isShowTotal="false"
      :info="fixedSubButtonInfo"
      btnTitle="确 定"
      @changeCheckAll="changeCheckAll"
      @confirm="confirmSelect"
    />
  </view>
</template>

<script>
import BaseNavbar from "../../components/base/BaseNavbar.vue";
import DeviceAdvertsCard from "../components/cards/DeviceAdvertsCard.vue";
import ComList from "../../components/list/ComList.vue";
import myPull from "../../mixins/myPull";
import BaseSearch from "../../components/base/BaseSearch.vue";
import FixedSubButton from "../../components/common/FixedSubButton.vue";
export default {
  components: {
    BaseNavbar,
    DeviceAdvertsCard,
    ComList,
    BaseSearch,
    FixedSubButton,
  },
  mixins: [myPull()],
  computed: {
    fixedSubButtonInfo() {
      let selectList = this.listData?.filter((item) => item.isCheck) || [];
      let isAllCheck =
        selectList.length === this.listData.length && selectList.length !== 0;
      return {
        isAllCheck, //是否全选
        selectNum: selectList.length || 0, //选中数量
      };
    },
  },
  data() {
    return {
      device_sn: "", //设备编号
      fromData: "",
      isFromAdSelectDevice: false,
    };
  },
  methods: {
    search(e) {
      this.device_sn = e;
      this.refresh();
    },
    getList(page, done) {
      let data = { page, device_sn: this.device_sn };
      this.$u.api.bindAdvertList(data).then((res) => {
        done(res);
      });
    },
    //点击全选
    changeCheckAll(isAllCheck) {
      this.listData = this.listData.map((item) => {
        if (typeof item.isCheck == "undefined") {
          this.$set(item, "isCheck", isAllCheck);
        } else {
          item.isCheck = isAllCheck;
        }
        return item;
      });
    },
    //确认选择
    confirmSelect() {
      let deviceList = this.listData?.filter((item) => item.isCheck) || [];
      let pages = getCurrentPages();
      let currPage = pages[pages.length - 1]; //当前页面
      let prevPage = pages[pages.length - 2]; //上一个页面
      //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
      prevPage.setData({
        ad_select_device: deviceList,
      });
      uni.navigateBack({ delta: 1 });
    },
    selectItem(item) {
      if (typeof item.isCheck == "undefined") {
        this.$set(item, "isCheck", true);
      } else {
        item.isCheck = !item.isCheck;
      }
    },
  },
  onLoad(opt) {
    this.fromData = opt?.from;
    if (opt?.from === "ad_select_device") {
      this.isFromAdSelectDevice = true;
    }
    this.refresh();
  },
  onShow(e) {
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1]; // 当前页
    if (currPage.data.isDoRefresh == true) {
      // 是否刷新
      currPage.data.isDoRefresh = false;
      this.refresh();
    }
  },
};
</script>
<style lang="scss">
page {
  background-color: $pageBgColor;
}
</style>
<style lang="scss" scoped>
</style>